package top.plgxs.basic.one;

/**
 * <p> try-catch-finally </p>
 *
 * @author Stranger.
 * @since 2022/9/7 16:23
 */
public class TryCatchFinally {
    public static void main(String[] args) {
        System.out.println(test());
        test2();
    }
    // finally块总会执行，并且是在return语句执行后但未返回之前执行
    private static int test() {
        int temp = 1;
        try {
            System.out.println(temp);
            return ++temp;
        } catch (Exception e) {
            System.out.println(temp);
            return ++temp;
        } finally {
            ++temp;
            System.out.println(temp);
        }
    }

    // 遇到System.exit(0)时，finally不一定执行
    private static void test2() {
        try {
            int i = 10 / 0;
        } catch (Exception e) {
            System.out.println("执行catch块");
            System.exit(0);
        } finally {
            System.out.println("执行finally块");
        }
    }

}
